<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('config.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('config.name')" prop="name">
            <el-input :placeholder="i18n('config.name')" v-model="dataForm.name"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('config.title')" prop="title">
            <el-input :placeholder="i18n('config.title')" v-model="dataForm.title"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('config.category_title')" prop="category_id">
            <el-cascader
              v-model="dataForm.category_id"
              :placeholder="i18n('common.please_select') + i18n('config.category_title')"
              :show-all-levels="false"
              :options="levellist"
              :props="levelProps"
              clearable>
            </el-cascader>
          </el-form-item>

          <el-form-item :label="i18n('config.type')" prop="type">
            <el-select v-model="dataForm.type" :placeholder="i18n('common.please_select') + i18n('config.type')">
              <el-option label="单行输入框" value="text"></el-option>
              <el-option label="数字输入项" value="number"></el-option>
              <el-option label="百分百数字输入项" value="percent"></el-option>
              <el-option label="多行输入框" value="textarea"></el-option>
              <el-option label="开关输入项" value="switch"></el-option>
              <el-option label="日期输入项" value="date"></el-option>
              <el-option label="时间输入项" value="time"></el-option>
              <el-option label="日期时间输入项" value="datetime"></el-option>
              <el-option label="起止时间输入项" value="daterange"></el-option>
              <el-option label="上传输入框" value="upload"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('config.value')" prop="value">
            <el-input
              type="textarea"
              :placeholder="i18n('common.please_input') + i18n('config.value')"
              maxlength="200"
              show-word-limit
              v-model="dataForm.value">
            </el-input>
          </el-form-item>

          <el-form-item :label="i18n('config.params')" prop="params">
            <el-input
              type="textarea"
              :placeholder="i18n('common.please_input') + i18n('config.params')"
              maxlength="200"
              show-word-limit
              v-model="dataForm.params">
            </el-input>
          </el-form-item>

          <el-form-item :label="i18n('config.content')" prop="content">
            <el-input
              type="textarea"
              :placeholder="i18n('common.please_input') + i18n('config.content')"
              maxlength="200"
              show-word-limit
              v-model="dataForm.content">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('kernel:config:handle')" type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="config-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('config')
  const levellist = ref([])

  const levelProps = reactive({
    label: 'name',
    value: 'id',
    checkStrictly: true,
  });

  const dataForm = reactive({
    id: 0,
    category_id: 0,
    name: '',
    title: '',
    type: '',
    value: '',
    params: '',
    content: '',
  })

  const dataRule = reactive({
    name: [{
      required: true,
      message: i18n('config.rules.name.require'),
      trigger: 'blur',
    }, {
      min: 1,
      max: 50,
      message: i18n('config.rules.name.length'),
      trigger: 'blur',
    }],
    title: [{
      required: true,
      message: i18n('config.rules.title.require'),
      trigger: 'blur',
    }, {
      min: 1,
      max: 50,
      message: i18n('config.rules.title.length'),
      trigger: 'blur',
    }],
    type: [{
      required: true,
      message: i18n('config.rules.type.require'),
      trigger: 'blur',
    }],
    value: [{
      required: true,
      message: i18n('config.rules.value.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/config/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.category_id = data.data.category_id
            dataForm.name = data.data.name
            dataForm.title = data.data.title
            dataForm.type = data.data.type
            dataForm.value = data.data.value
            dataForm.params = data.data.params
            dataForm.content = data.data.content
          }
        })
      }
    })
  }

  const loadLevelList = () => {
    $http({
      url: $http.adornUrl('/config/category/level'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        levellist.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    init(formRef)
    loadLevelList()
  })
</script>
